package incr_device

import java.text.MessageFormat.format
import  org.apache.spark.sql.types._
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.spark.{SparkConf, SparkContext}
import org.json4s.jackson.JsonMethods._
import util.Util._

object Month_incr_device {
   def main(args: Array[String]) {
     // Validate args
     if (args.length == 0) {
       println("Usage: [2015-05-25|date]")
       sys.exit(1)
     }
     val date_str=args(0).toString
     //val date_st="2015-05-24"
     val filePath=getConfig("incr_device.hdfsdir")+"e_device_activated_count_month/"+getMonth(date_str)

     val sparkConf=new SparkConf().setAppName("month_incr_device")
     val sc= new SparkContext(sparkConf)
     val hdfs=FileSystem.get(new Configuration())
     if(hdfs.exists(new Path(filePath)))hdfs.delete(new Path(filePath),true)
     val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc)

     import sqlContext._

     val week_ac_dev_sql= format(getConfig("incr_device.month"),conv2ts(addDay(date_str,1).toString).toString,conv2ts(MonthofYear(date_str).toString).toString)
     val pk_sql=format(getConfig("active_device.pkmap"))
     val  df=sqlContext.sql(week_ac_dev_sql)
     val pk=sqlContext.sql(pk_sql)
     val pkmap=pk.distinct.map(row=>row.mkString("@#"))
       .subtract( df.select("product_key")
       .distinct.map(product_key=>product_key.mkString("@#")))
       .map(product_key=>product_key.toString
       +"@#"+product_key.toString+"%" +"Unknown"
       +"@#"+product_key.toString+"%" +"Unknown"+"%" +"Unknown"
       +"@#"+product_key.toString+"%" +"Unknown"+"%" +"Unknown"+"%" +"Unknown"
       +"@#"+product_key.toString+"%" +"Unknown"+"%" +"Unknown"+"%" +"Unknown"+"%" +"Unknown"
       +"@#"+product_key.toString+"%"+"location"+"%"+"Unknown"
       +"@#"+product_key.toString+"%"+"location"+"%"+"Unknown"+"%"+"Unknown"
       +"@#"+product_key.toString+"%"+"location"+"%"+"Unknown"+"%"+"Unknown"+"%"+"Unknown"
       )
       .flatMap(line=>line.split("@#"))
       .map(line=>(line.split("%")(0),(line,0L)))

     df.map(row=>row.mkString("@#")).map(line=>line.split("@#")).map(
       line=>line(0).toString
         +"@#"+line(0)+"%"+replaceNull(line(4))
         +"@#"+line(0)+"%"+replaceNull(line(4))+"%"+replaceNull(line(1))
         +"@#"+line(0)+"%"+replaceNull(line(4))+"%"+replaceNull(line(1))+"%"+replaceNull(line(2))
         +"@#"+line(0)+"%"+replaceNull(line(4))+"%"+replaceNull(line(1))+"%"+replaceNull(line(2))+"%"+replaceNull(line(3))
         +"@#"+line(0)+"%"+"location"+"%"+replaceNull(line(1))
         +"@#"+line(0)+"%"+"location"+"%"+replaceNull(line(1))+"%"+replaceNull(line(2))
         +"@#"+line(0)+"%"+"location"+"%"+replaceNull(line(1))+"%"+replaceNull(line(2))+"%"+replaceNull(line(3))
     ).flatMap(line =>line.split("@#"))
         .map(line=>(line,1L))
         .reduceByKey(_ + _)
         .map(line=>(line._1.split("%")(0),line))
         .++(pkmap)
         .map(t=>(t._1,toJobejct(t._2,getMonth(date_str).toString)))
         .reduceByKey((x,y)=>x merge y)
         .map(line=>compact(render(line._2)))
         .coalesce(1, shuffle = true)
         .saveAsTextFile(filePath)

   }
 }
